package com.example.voting_master.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.voting_master.constant.RedisConstant;
import com.example.voting_master.entity.CandidateVoteStats;
import com.example.voting_master.entity.VoteTheme;
import com.example.voting_master.mapper.CandidateVoteStatsMapper;
import com.example.voting_master.mapper.VoteThemeMapper;
import com.example.voting_master.service.CandidateVoteStatsService;
import com.example.voting_master.util.RedisCacheUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
public class CandidateVoteStatsServiceImpl
    extends ServiceImpl<CandidateVoteStatsMapper, CandidateVoteStats>
    implements CandidateVoteStatsService {

    @Resource
    RedisCacheUtil redisCacheUtil;

    @Resource
    VoteThemeMapper voteThemeMapper;

    /**
     * 根据投票记录表获取所有候选人票数
     * @return
     */
    @Override
    public List<CandidateVoteStats> getAllCandidatesVotes(){

        List<CandidateVoteStats> candidateVoteStatsList = baseMapper.selectCandidatesVotes();
        return candidateVoteStatsList;
    }

    /**
     * 更新候选人投票状态表的候选人票数
     */
    @Override
    public void updateAllCandidatesVotes(){
        List<CandidateVoteStats> candidateVoteStatsList = this.getAllCandidatesVotes();
        /*String redisKey = RedisConstant.Theme + "all:";*/

        for (CandidateVoteStats can:candidateVoteStatsList) {

            //更新数据库表
            LambdaUpdateWrapper<CandidateVoteStats> lambdaUpdateWrapper = new UpdateWrapper<CandidateVoteStats>().lambda();
            lambdaUpdateWrapper.eq(CandidateVoteStats::getCandidateId,can.getCandidateId()).eq(CandidateVoteStats::getThemeId,can.getThemeId());
            baseMapper.update(can,lambdaUpdateWrapper);

        }

    }

}
